Return to start page
Core/String/Library Pool.j
1 /**
2 * Provides many functions for working with character pools and checking strings and characters for their content.
3 * @author Tamino Dauth
4 */
5 library ALibraryCoreStringPool requires ALibraryCoreStringMisc
6
7 globals
8 constant string AAlphabeticalCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
9 constant string ANumeralCharacters = "0123456789"
10 constant string ASpecialCharacters = "!§$%&/()=?+-/*,.-;:_~#'|<>äöüß"
11 constant string AWhiteSpaceCharacters = " \t"
12 constant string ASignatureCharacters = "+-"
13 constant string ABinaryCharacters = "01"
14 constant string AOctalCharacters = "01234567"
15 constant string AHexadecimalCharacters = "0123456789ABCDEF"
16 endglobals
17
18 /**
19 * Generates a random character from the character pool @param charPool.
20 * @param charPool Character pool which is used for generation of a random character.
21 * @return Returns a random character from the character pool @param charPool.
22 */
23 function GetRandomCharacter takes string charPool returns string
24 local integer randomInteger = GetRandomInt(1, StringLength(charPool))
25 return SubString(charPool, randomInteger - 1, randomInteger)
26 endfunction
27
28 /**
29 * Generates a random alphabetical character.
30 * @return Returns a random alphabetical character.
31 */
32 function GetRandomAlphabeticalCharacter takes nothing returns string
33 return GetRandomCharacter(AAlphabeticalCharacters)
34 endfunction
35
36 /**
37 * Generates a random numeral character.
38 * @return Returns a random numeral character.
39 */
40 function GetRandomNumeralCharacter takes nothing returns string
41 return GetRandomCharacter(ANumeralCharacters)
42 endfunction
43
44 /**
45 * Generates a random special character.
46 * @return Returns a random special character.
47 */
48 function GetRandomSpecialCharacter takes nothing returns string
49 return GetRandomCharacter(ASpecialCharacters)
50 endfunction
51
52 /**
53 * Generates a random white-space character.
54 * @return Returns a random white-space character.
55 */
56 function GetRandomWhiteSpaceCharacter takes nothing returns string
57 return GetRandomCharacter(AWhiteSpaceCharacters)
58 endfunction
59
60 /**
61 * Generats a random string with the length @param length.
62 * Is able to include various other characters.
63 * @param length The length of the returned value.
64 * @param includeNumberCharacters If this value is true number charactes will be added into the string pool.
65 * @param includeSpecialCharacters If this value is true special characters will be added into the string pool.
66 * @param includeWhiteSpaceCharacters If this value is true white-space characters will be added into the string pool.
67 * @return Returns a random string generated from the selected string pool.
68 */
69 function GetRandomString takes integer length, boolean includeNumberCharacters, boolean includeSpecialCharacters, boolean includeWhiteSpaceCharacters returns string
70 local integer i
71 local string characters = AAlphabeticalCharacters
72 local string result = ""
73 if (includeNumberCharacters) then
74 set characters = characters + ANumeralCharacters
75 endif
76 if (includeSpecialCharacters) then
77 set characters = characters + ASpecialCharacters
78 endif
79 if (includeWhiteSpaceCharacters) then
80 set characters = characters + AWhiteSpaceCharacters
81 endif
82 set i = 0
83 loop
84 exitwhen (i == length)
85 set result = result + GetRandomCharacter(characters)
86 set i = i + 1
87 endloop
88 return result
89 endfunction
90
91 /**
92 * Checks if string @param whichString contains only characters from character pool @param characterPool.
93 * Checks each single character.
94 * @param whichString String which should be checked.
95 * @param characterPool Character pool which will be used for comparing all characters.
96 * @return Returns if all characters from string @param whichString are contained by string pool @param characterPool.
97 */
98 function IsStringFromCharacterPool takes string whichString, string characterPool returns boolean
99 local integer i
100 set i = 1
101 loop
102 exitwhen (i > StringLength(whichString))
103 if (FindString(characterPool, SubString(whichString, i - 1, i)) == -1) then
104 return false
105 endif
106 set i = i + 1
107 endloop
108 return true
109 endfunction
110
111 /**
112 * Checks if string @param whichString is alphabetical.
113 * @param whichString Checked string.
114 * @return Returns true if string @param whichString contains only alphabetical characters.
115 */
116 function IsStringAlphabetical takes string whichString returns boolean
117 return IsStringFromCharacterPool(whichString, AAlphabeticalCharacters)
118 endfunction
119
120 /**
121 * Checks if string @param whichString is numeral.
122 * @param whichString Checked string.
123 * @return Returns true if string @param whichString contains only numeral characters.
124 */
125 function IsStringNumeral takes string whichString returns boolean
126 return IsStringFromCharacterPool(whichString, ANumeralCharacters)
127 endfunction
128
129 /**
130 * Checks if string @param whichString contains only special characters.
131 * @param whichString Checked string.
132 * @return Returns true if string @param whichString contains only special characters.
133 */
134 function IsStringSpecial takes string whichString returns boolean
135 return IsStringFromCharacterPool(whichString, ASpecialCharacters)
136 endfunction
137
138 /**
139 * Checks if string @param whichString contains only white-space characters.
140 * @param whichString Checked string.
141 * @return Returns true if string @param whichString contains only white-space characters.
142 */
143 function IsStringWhiteSpace takes string whichString returns boolean
144 return IsStringFromCharacterPool(whichString, AWhiteSpaceCharacters)
145 endfunction
146
147 /**
148 * Checks if string @param whichString is a signature.
149 * @param whichString Checked string.
150 * @return Returns true if string @param whichString is a signature.
151 */
152 function IsStringSignature takes string whichString returns boolean
153 return IsStringFromCharacterPool(whichString, ASignatureCharacters)
154 endfunction
155
156 /**
157 * Checks if string @param which is an integer.
158 * @param which Checked string.
159 * @return Returns true if string @param which is an integer.
160 */
161 function IsStringInteger takes string whichString returns boolean
162 if (IsStringSignature(SubString(whichString, 0, 1))) then
163 if (StringLength(whichString) > 1) then
164 return IsStringNumeral(SubString(whichString, 1, StringLength(whichString)))
165 endif
166 return false
167 endif
168 return IsStringNumeral(SubString(whichString, 0, StringLength(whichString)))
169 endfunction
170
171 /**
172 * Bei den folgenden Zahlentypen muss die Zahl immer mit einer bestimmten Ziffernfolge beginnen.
173 * Ansonsten wird sie NICHT als eine solche Zahl erkannt.
174 * Dies ist eine Anlehnung an C++ und wahrscheinlich auch an vielen andere Sprachen:
175 * - 0% binär
176 * - 0 oktal
177 * - 0x hexadezimal
178 */
179
180 /**
181 * Checks if string @param whichString is binary.
182 * @param whichString Checked string.
183 * @return Returns true if string @param whichString is binary.
184 */
185 function IsStringBinary takes string whichString returns boolean
186 if (StringLength(whichString) >= 2 and SubString(whichString, 0, 2) == "0%") then
187 if (StringLength(whichString) > 2) then
188 return IsStringFromCharacterPool(SubString(whichString, 2, StringLength(whichString)), ABinaryCharacters)
189 endif
190 return false
191 endif
192 return false
193 endfunction
194
195 /**
196 * Checks if string @param whichString is octal.
197 * @param whichString Checked string.
198 * @return Returns true if string @param whichString is octal.
199 */
200 function IsStringOctal takes string whichString returns boolean
201 if (StringLength(whichString) >= 1 and SubString(whichString, 0, 1) == "0") then
202 if (StringLength(whichString) > 1) then
203 return IsStringFromCharacterPool(SubString(whichString, 1, StringLength(whichString)), AOctalCharacters)
204 endif
205 return false
206 endif
207 return false
208 endfunction
209
210 /**
211 * Checks if string @param whichString is hexadecimal.
212 * @param whichString Checked string.
213 * @return Returns true if string @param whichString is hexadecimal.
214 */
215 function IsStringHexadecimal takes string whichString returns boolean
216 if (StringLength(whichString) >= 2 and SubString(whichString, 0, 2) == "0x") then
217 if (StringLength(whichString) > 2) then
218 return IsStringFromCharacterPool(SubString(whichString, 2, StringLength(whichString)), AHexadecimalCharacters)
219 endif
220 return true
221 endif
222 return false
223 endfunction
224
225 endlibrary